﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IntelliTouchMix
{
    public partial class frmMain : DevComponents.DotNetBar.Office2007RibbonForm
    {

        private void initUserCode()
        {
        }

        private void loadUserCode()
        {
            Util.master_device = 0;
            Util.master_speakers = 0;
            Util.monitor_device = 0;
            Util.monitor_speakers = 0;

            addSubItems(buttonItem91, formMain.getCDDrives(), cdDriveChangedHandler);
            List<String> devices = formMain.getAudioDevices();

            if (devices.Count > 0)
            {
                addSubItems(buttonItem145, devices, audioDeviceChanged1);
                addSubItems(buttonItem2, devices, audioDeviceChanged2);
                addSubItems(buttonItem142, formMain.getAudioSpeakers(0), speakersChanged1);
                addSubItems(buttonItem3, formMain.getAudioSpeakers(0), speakersChanged2);
            }
            removeSubItems(buttonItem107);
            addSubItems(buttonItem107, Util.listWaveForm, waveFormChanged);
            removeSubItems(buttonItem7);
            addSubItems(buttonItem7, Util.listEffectMaterials, materialEffectChanged);
            removeSubItems(buttonItem4);
            addSubItems(buttonItem4, Util.listEffects, effect2FingersChanged);
            removeSubItems(buttonItem6);
            addSubItems(buttonItem6, Util.listEffects, effect3FingersChanged);
            removeSubItems(buttonItem81);
            addSubItems(buttonItem81, Util.listTransitionTime, transitionTimeChanged);

            Util.controllContourPosition[Util.DECK_A][Util.DECK_WAVE_FULL] = Util.deckA.panelWave;
            Util.controllContourPosition[Util.DECK_A][Util.DECK_WAVE_SCROLL] = Util.deckA.panelScroll;
            Util.controllContourPosition[Util.DECK_A][Util.DECK_VOLUME] = Util.deckA.PanelVolume;
            Util.controllContourPosition[Util.DECK_A][Util.DECK_TEMPO] = Util.deckA.panelTempo;
            Util.controllContourPosition[Util.DECK_A][Util.DECK_RATE] = Util.deckA.panelRate;

            Util.controllContourPosition[Util.DECK_B][Util.DECK_WAVE_FULL] = Util.deckB.panelWave;
            Util.controllContourPosition[Util.DECK_B][Util.DECK_WAVE_SCROLL] = Util.deckB.panelScroll;
            Util.controllContourPosition[Util.DECK_B][Util.DECK_VOLUME] = Util.deckB.PanelVolume;
            Util.controllContourPosition[Util.DECK_B][Util.DECK_TEMPO] = Util.deckB.panelTempo;
            Util.controllContourPosition[Util.DECK_B][Util.DECK_RATE] = Util.deckB.panelRate;

            Util.controllContourPosition[Util.LIBRARY][Util.LIBRARY_SELECTED] = Util.playList.panelPlayList;
            Util.controllContourPosition[Util.LIBRARY][Util.LIBRARY_EFFECT_BAR] = Util.playList.panelEffects;

            Util.controllContourPosition[Util.INDICATORS][Util.INDICATORS_2_FINGERS_CATEGORY] = Util.indicatorForm.twoFinger.imgCenter;
            Util.controllContourPosition[Util.INDICATORS][Util.INDICATORS_2_FINGERS_EFFECT] = Util.indicatorForm.twoFinger.imgSonCenter;

            Util.formMain.GSlider_Rate1 = Util.deckA.GSlider_Rate1;
            Util.formMain.GSlider_Tempo1 = Util.deckA.GSlider_Tempo1;
            Util.formMain.GSlider_Rate2 = Util.deckB.GSlider_Rate1;
            Util.formMain.GSlider_Tempo2 = Util.deckB.GSlider_Tempo1;

            Util.formMain.pictureBoxWaveformFull1 = Util.deckA.pictureBoxDeckWaveformFull1;
            Util.formMain.pictureBoxWaveformFull2 = Util.deckB.pictureBoxDeckWaveformFull1;
            Util.formMain.pictureBoxWaveformScroll1 = Util.deckA.pictureBoxDeckWaveformScroll1;
            Util.formMain.pictureBoxWaveformScroll2 = Util.deckB.pictureBoxDeckWaveformScroll1;
            Util.formMain.axGSlider1 = Util.playList.axGSlider1;

            Util.formMain.PlayList1 = Util.playList.PlayList1;
        }

        public Int16 getSelectedIndex(object sender)
        { 
            return Int16.Parse(((DevComponents.DotNetBar.ButtonItem)sender).Name.Split('_').Last());
        }

        void transitionTimeChanged(object sender, EventArgs e)
        {
            Int16 index = getSelectedIndex(sender);
        }

        void effect2FingersChanged(object sender, EventArgs e)
        {
            Int16 index = getSelectedIndex(sender);
        }

        void effect3FingersChanged(object sender, EventArgs e)
        {
            Int16 index = getSelectedIndex(sender);
        }

        void materialEffectChanged(object sender, EventArgs e)
        {
            Int16 index = getSelectedIndex(sender);
        }

        void waveFormChanged(object sender, EventArgs e)
        {
            int selected = getSelectedIndex(sender);
            if (selected == 0)
            {
                formMain.OptionWaveform_CheckedChanged(new object(), EventArgs.Empty);
            }
            else if (selected == 1)
            {
                formMain.OptionSpectrum_CheckedChanged(new object(), EventArgs.Empty);
            }
            else if (selected == 2)
            {
                formMain.OptionOscill_CheckedChanged(new object(), EventArgs.Empty);
            }
            
        }

        void removeSubItems(DevComponents.DotNetBar.BaseItem item)
        {
            item.SubItems.Clear();
        }

        void addSubItems(DevComponents.DotNetBar.BaseItem item,List<String> list,EventHandler eventHandler)
        {
            for (int i = 0; i < list.Count; i++)
            {
                DevComponents.DotNetBar.ButtonItem bi = new DevComponents.DotNetBar.ButtonItem(item.Name+"Item_" + i.ToString(), list[i]);
                bi.OptionGroup = item.Name + "Items";
                bi.Click += eventHandler;
                item.SubItems.Add(bi);
            }
        }

        void cdDriveChangedHandler(object sender, EventArgs e)
        {
            formMain.loadCD(getSelectedIndex(sender));
        }

        void audioDeviceChanged1(object sender, EventArgs e)
        {
            removeSubItems(buttonItem142);
            addSubItems(buttonItem142, formMain.getAudioSpeakers(getSelectedIndex(sender)), speakersChanged1);
            Util.master_device = getSelectedIndex(sender);
            formMain.changeConfig();
        }

        void audioDeviceChanged2(object sender, EventArgs e)
        {
            removeSubItems(buttonItem3);
            addSubItems(buttonItem3, formMain.getAudioSpeakers(getSelectedIndex(sender)), speakersChanged2);
            Util.monitor_device = getSelectedIndex(sender);
            formMain.changeConfig();
        }

        void speakersChanged1(object sender, EventArgs e)
        {
            Util.master_speakers = getSelectedIndex(sender);
            formMain.changeConfig();
        }

        void speakersChanged2(object sender, EventArgs e)
        {
            Util.monitor_speakers = getSelectedIndex(sender);
            formMain.changeConfig();
        }

        private void buttonItem97_Click(object sender, EventArgs e)
        {
            formMain.actionPlay();
        }

        private void buttonItem102_Click(object sender, EventArgs e)
        {
            formMain.actionStop();
        }

        private void buttonItem96_Click(object sender, EventArgs e)
        {
            formMain.actionLast();
        }

        private void buttonItem98_Click(object sender, EventArgs e)
        {
            formMain.actionNext();
        }

        private void buttonItem99_Click(object sender, EventArgs e)
        {
            formMain.actionPause();
        }

        private void buttonItem92_Click(object sender, EventArgs e)
        {
            /*
            Form1PL Pl = new Form1PL();
            Pl.Show();

            FormMain f = new FormMain();
             * */
            formMain.BtnList1Load_Click(new object(), EventArgs.Empty);
        }

        public void buttonItem175_Click(object sender, EventArgs e)
        {
            this.formMain.BtnAddPL1_Click(new object(), EventArgs.Empty);
        }

        private void buttonItem173_Click(object sender, EventArgs e)
        {
            Internet In = new Internet(this);
            // MessageBox.Show("apapa");
            In.Show();
        }

        private void buttonItem79_Click(object sender, EventArgs e)
        {
            Internet In = new Internet(this);
            In.Show();
        }


    }
}
